package com.rc.evcharger.service.order;

import com.rc.evcharger.common.enums.CommanderEnum;
import com.rc.evcharger.mapper.elec.ElecPileMapper;
import com.rc.evcharger.mapper.elec.ElecStationMapper;
import com.rc.evcharger.mapper.hlhtbill.HlhtTenantOrderNotifyMapper;
import com.rc.evcharger.mapper.order.OrderCommentMapper;
import com.rc.evcharger.model.elec.ElecGun;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.model.elec.ElecStation;
import com.rc.evcharger.model.hlhtbill.HlhtTenantOrderNotify;
import com.rc.evcharger.model.order.OrderComment;
import com.rc.evcharger.model.order.OrderCommentExample;
import com.rc.evcharger.service.elec.ElecGunService;
import com.rc.evcharger.vo.charge.StartChargeReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by sven on 2019/09/23.
 */
@Service
public class OrderCommentServiceImpl implements OrderCommentService {

    @Autowired
    private OrderCommentMapper orderCommentMapper;

    @Autowired
    private HlhtTenantOrderNotifyMapper hlhtTenantOrderNotifyMapper;

    @Autowired
    private ElecPileMapper elecPileMapper;

    @Autowired
    private ElecGunService elecGunService;

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Override
    public OrderCommentMapper getMapper() {
        return orderCommentMapper;
    }

    @Override
    public OrderComment selectByOrderNum(String orderNum) {
        OrderCommentExample orderCommentExample = new OrderCommentExample();
        orderCommentExample.createCriteria().andOrderNumEqualTo(orderNum);
        List<OrderComment> orderComments = orderCommentMapper.selectByExample(orderCommentExample);
        if (orderComments == null || orderComments.size() == 0) {
            return null;
        }
        OrderComment orderComment = orderComments.get(0);
        return orderComment;
    }

    @Override
    public OrderComment selectByGunId(Long gunId) {
        OrderCommentExample orderCommentExample = new OrderCommentExample();
        orderCommentExample.createCriteria().andElecGunIdEqualTo(gunId);
        orderCommentExample.setOrderByClause(" comment_id  DESC LIMIT 1 ");
        List<OrderComment> orderComments = orderCommentMapper.selectByExample(orderCommentExample);
        if (orderComments == null || orderComments.size() == 0) {
            return null;
        }
        OrderComment orderComment = orderComments.get(0);
        return orderComment;
    }


    /**
     * 启动充电公共方法插入
     *
     * @param startChargeReq
     * @return
     */
    @Override
    public OrderComment insertStartCharge(StartChargeReq startChargeReq, String orderNum) {

        ElecPile elecPile = elecPileMapper.selectByPrimaryKey(startChargeReq.getElecPileId());
        ElecGun elecGun = elecGunService.validGun(elecPile, startChargeReq.getGunNum());

        ElecStation elecStation = elecStationMapper.selectByPrimaryKey(elecPile.getElecStationId());

        OrderComment orderComment = new OrderComment();
        orderComment.setTenantCode(startChargeReq.getTenantCode());   //运营商ID
        orderComment.setTenantOperatorCode(elecStation.getTenantOperatorCode());//分销商
        orderComment.setHlhtTenantCode(startChargeReq.getHlhtTenantCode());//SAAS互联互通运营商
        orderComment.setMemberTicketIds(startChargeReq.getMemberTicketIds()); //优惠券
        orderComment.setIsUseGiveAmount(startChargeReq.getIsUseGiveAmount());
        //互联互通的operatorID
        if (StringUtils.isNotBlank(startChargeReq.getHlhtOperatorId())) {
            orderComment.setHlhtOperatorId(startChargeReq.getHlhtOperatorId());
        }

        //各种类型的充电方式: //互联互通订单类型以tenant_code为基准,0为自主订单 1反向对接订单 2正向对接订单, 默认0
        if (StringUtils.isBlank(startChargeReq.getHlhtTenantCode()) && StringUtils.isBlank(startChargeReq.getHlhtOperatorId())) {//运营商自己的桩
            orderComment.setHlhtOrderType("0");
        } else if (StringUtils.isNotBlank(startChargeReq.getHlhtTenantCode()) && StringUtils.isBlank(startChargeReq.getHlhtOperatorId())) {//其它SAAS运营商桩
            orderComment.setHlhtOrderType("2");
        } else {//第三方运营商
            orderComment.setHlhtOrderType("1");
        }

        orderComment.setElecStationId(elecPile.getElecStationId());
        orderComment.setElecPileId(elecPile.getElecPileId());
        orderComment.setElecGunId(elecGun.getElecGunId());
        orderComment.setCreatedTime(new Date());

        orderComment.setOrderNum(orderNum);

        //保存外部订单号
        if (StringUtils.isBlank(startChargeReq.getOutOrderNum())) {
            orderComment.setOutOrderNum(orderNum);
        } else {
            orderComment.setOutOrderNum(startChargeReq.getOutOrderNum());
        }

        orderComment.setMemberId(startChargeReq.getMemberId());
        orderComment.setAccountType(startChargeReq.getAccountType()); //支付的账户类型 0个人钱包 1企业钱包 3.先付后退 默认0
        orderComment.setOrderSource(startChargeReq.getOrderSource()); //订单来源 0 公众号 1 APP  2小程序 3.刷卡 4.第三方API  默认0
        orderComment.setTicketUsingType(startChargeReq.getTicketUsingType());//优先使用优惠券,默认1使用 0不使用
        orderComment.setCarNum(startChargeReq.getCarNum());

        orderComment.setStartType(startChargeReq.getStartType()); //启动方式 0 一般 1 刷卡 2 VIN码启动 默认0

        orderComment.setChargingPolicy(startChargeReq.getStartType()); //充电策略与参数
        orderComment.setChargingPolicyParm(startChargeReq.getStartValue());

        orderCommentMapper.insertSelective(orderComment);

        return orderComment;
    }
}
